<?php

namespace Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @Gedmo\Tree(type="nested")
 * @ORM\Entity(repositoryClass="Entity\Repository\UserRepository")
 * @ORM\Table(name="user")
 */
class User extends MappedSuperclassBase
{

    /**
     * @ORM\Column(name="bonus", type="integer")
     */
    protected $bonus;

    /**
     * @Gedmo\TreeLeft
     * @ORM\Column(type="integer")
     */
    protected $lft;

    /**
     * @Gedmo\TreeRight
     * @ORM\Column(type="integer")
     */
    protected $rgt;

    /**
     * @Gedmo\TreeParent
     * @ORM\ManyToOne(targetEntity="User", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
     */
    protected $parent;

    /**
     * @Gedmo\TreeRoot
     * @ORM\Column(type="integer", nullable=true)
     */
    protected $root;

    /**
     * @Gedmo\TreeLevel
     * @ORM\Column(name="lvl", type="integer")
     */
    protected $level;

    /**
     *
     * @ORM\ManyToOne(targetEntity="Group", inversedBy="users")
     */
    protected $group;

    /**
     *
     * @var integer
     * @ORM\Column(type="string")
     */
    protected $name;

    /**
     *
     * @var integer
     * @ORM\Column(type="string", name="full_name", nullable= true)
     */
    protected $fullName;

    /**
     *
     * @var integer
     * @ORM\Column(type="string", nullable= true)
     */
    protected $password;

    /**
     *
     * @var integer
     * @ORM\Column(type="string", nullable= true)
     */
    protected $email;

    /**
     *
     * @var datetime
     * @ORM\Column(type="datetime", name="date_create", nullable= true)
     * @Gedmo\Timestampable(on="create")
     */
    protected $dateCreate;

    /**
     *
     * @var datetime
     * @ORM\Column(type="datetime", name="date_update", nullable= true)
     * @Gedmo\Timestampable(on="update")
     */
    protected $dateUpdate;

    /**
     *
     * @var datetime
     * @ORM\Column(type="datetime", name="last_visit", nullable= true)
     */
    protected $lastVisit;

    /**
     *
     * @var integer
     * @ORM\Column(type="decimal", precision=10, scale=2, nullable= true)
     */
    protected $balance;

    /**
     *
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="User", mappedBy="parent")
     *
     */
    protected $children;

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="SimOwner", mappedBy="user", cascade={"persist", "remove"})
     */
    protected $sims;

    /**
     *
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="SimOwner", mappedBy="user")
     */
    protected $balances;

    /**
     *
     * @var datetime
     * @ORM\Column(type="datetime", name="last_balance_up", nullable= true)
     */
    protected $lastBalanceUp;

    /**
     *
     * @var type 
     * @ORM\OneToMany(targetEntity="UserHistory", mappedBy="user", cascade={"persist", "remove"})
     */
    protected $historyLogin;

    function __construct(array $data = null)
    {
        $this->children = new ArrayCollection;
        $this->sims = new ArrayCollection;
        $this->historyLogin = new ArrayCollection();
        $this->lastVisit = new \DateTime;
        $this->dateUpdate = new \DateTime;
        $this->bonus = 0;
        parent::__construct($data);
    }

    public function getLevel()
    {
        return $this->level;
    }

    public function getLeft()
    {
        return $this->lft;
    }

    public function getRight()
    {
        return $this->rgt;
    }

    public function getRoot()
    {
        return $this->root;
    }

    public function getParent()
    {
        return $this->parent;
    }

    public function setParent(User $parent = null)
    {
        $this->parent = $parent;
        return $this;
    }

    /**
     * 
     * @return Group
     */
    public function getGroup()
    {
        return $this->group;
    }

    public function setGroup(Group $group)
    {
        $this->group = $group;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getFullName()
    {
        return $this->fullName;
    }

    public function setFullName($fullName)
    {
        $this->fullName = $fullName;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function setPassword($password)
    {
        $this->password = $password;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function getDateCreate()
    {
        return $this->dateCreate;
    }

    public function setDateCreate($dateCreate)
    {
        $this->dateCreate = $dateCreate;
    }

    public function getDateUpdate()
    {
        return $this->dateUpdate;
    }

    public function setDateUpdate($dateUpdate)
    {
        $this->dateUpdate = $dateUpdate;
    }

    public function getLastVisit()
    {
        return $this->lastVisit;
    }

    public function setLastVisit(\DateTime $lastVisit)
    {
        $this->lastVisit = $lastVisit;
    }

    public function getIp()
    {
        return $this->ip;
    }

    public function setIp($ip)
    {
        $this->ip = $ip;
    }

    public function getBalance()
    {
        return $this->balance;
    }

    public function setBalance($balance)
    {
        $this->balance = $balance;
    }

    public function getBonus()
    {
        return $this->bonus;
    }

    public function setBonus($bonus)
    {
        $this->bonus = $bonus;
    }

    /**
     *
     * @return ArrayCollection
     */
    public function getChildren()
    {
        return $this->children;
    }

    public function setChildren(ArrayCollection $children)
    {
        $this->children = $children;
    }

    public function addChildren(User $user)
    {
        $this->children->add($user);
        $user->setParent($this);
    }

    /**
     *
     * @return ArrayCollection
     */
    public function getSims()
    {
        return $this->sims;
    }

    public function getActiveSims()
    {
        $result = new ArrayCollection();
        foreach ($this->sims as $sim) {
            if ($sim->getState()) {
                $result->add($sim);
            }
        }
        return $result;
    }

    public function setSims(ArrayCollection $sims)
    {
        $this->sims = $sims;
    }

    public function addSim(Sim $sim)
    {
        $simOwner = new SimOwner();
        $simOwner->setSim($sim);
        $this->sims->add($simOwner);
        $simOwner->setUser($this);
    }

    public function isAdmin()
    {
        return $this->getGroup()->getName() == 'Смартсим';
    }

    public function getInfo()
    {
        return array(
            'name' => $this->name
        );
    }

    public function getSimByNumber($number)
    {
        $sims = $this->getActiveSims();
        foreach ($sims as $sim) {
            if ($sim->getSim()->getNumber() == $number) {
                return $sim;
            }
        }

        return null;
    }

    public function addBalance($sum)
    {
        $this->setBalance($this->getBalance() + $sum);
        $this->lastBalanceUp = new \DateTime;
        return $this;
    }

    public function getHistoryLogin()
    {
        return $this->historyLogin;
    }

    public function setHistoryLogin(ArrayCollection $historyLogin)
    {
        $this->historyLogin = $historyLogin;
    }

    public function addHistoryLogin(UserHistory $historyLogin)
    {
        $historyLogin->setUser($this);
        $this->historyLogin->add($historyLogin);
    }

}